﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain;
using Persistence;
using Persistence.Dao;
namespace web.member
{
    public partial class _default : System.Web.UI.Page
    {
        Domain.User user = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                user = (Domain.User)Session["user"];
                if (!IsPostBack)
                {
                    txtTrueName.Value = user.trueName;
                    txtAddress.Value = user.addressCorp;
                    txtEmail.Value = user.email;
                    txtMobile.Value = user.mobile;
                    txtTel.Value = user.telephone;
                    txtPostCode.Value = user.postCode == 0 ? "" : user.postCode.ToString();
                }
            }
            else
            {
                Response.Redirect("/login.aspx?from=/member/default.aspx");
            }
        }

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            if (txtPassword1.Value.Trim() == txtPassword2.Value.Trim())
            {
                Domain.User u = user;
                u.addressCorp = txtAddress.Value.Trim();
                u.email = txtEmail.Value.Trim();
                u.mobile = txtMobile.Value.Trim();
                if(txtPassword1.Value.Trim()!="")
                    u.password = txtPassword1.Value.Trim();
                u.postCode = Common.Utils.StrToInt(txtPostCode.Value.Trim(), 0);
                u.telephone = txtTel.Value.Trim();
                u.trueName = txtTrueName.Value.Trim();
                if (u.telephone.Length == 0 && u.mobile.Length == 0)
                {
                    Common.Message.alert("联系电话和手机请至少输入一个");
                    return;
                }
                Singleton<UserDao>.Instance.Update(u);
                Session["user"] = u;
                Common.Message.alertAndRedirect("修改注册用户信息成功", "/default.aspx");
            }
            else
            {
                Common.Message.alert("两次密码不一致，请重新输入");
            }
        }
    }
}
